package com.baomidou.ant.utils;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName: HttpFilter
 * @Description: TODO 自定义的HttpFilter，实现 Filter 接口
 * @author: zhoubeibei
 * @date: 2022年3月28日 上午10:05:01
 * @version V1.0
 */
public abstract class HttpFilter implements Filter {

    // 用于保存FilterConfig对象
    private FilterConfig FilterConfig;

    /**
     * <p>
     * Title: init
     * </p>
     * <p>
     * Description: 不建议子类直接覆盖，若直接覆盖，将可能会导致filterConfig成员变量初始化失败。
     * </p>
     *
     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
     * @param filterConfig
     * @throws ServletException
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

        this.FilterConfig = filterConfig;
        init();

    }

    /**
     * @Title: init
     * @Description: TODO 供子类继承的初始化方法。可以通过getFilterConfig() 获取FilterConfig对象。
     * @return void
     */
    protected void init() {
    }

    /**
     * @Title: getFilterConfig
     * @Description: TODO 直接返回init(ServletConfig)的FilterConfig对象
     * @return
     * @return FilterConfig
     */
    public FilterConfig getFilterConfig() {
        return FilterConfig;
    }

    /**
     * <p>
     * Title: destroy
     * </p>
     * <p>
     * Description: 空地destroy方法。
     * </p>
     *
     * @see javax.servlet.Filter#destroy()
     */
    @Override
    public void destroy() {

    }

    /**
     * <p>
     * Title: doFilter
     * </p>
     * <p>
     * Description: 原生的doFilter方法，在方法内部把ServletRequest和ServletResponse
     * 转为了HttpServletRequest和HttpServletResponse，并调用了 doFilter(HttpServletRequest
     * request, HttpServletResponse response, FilterChain chain)
     *
     * 若编写Filter的过滤方法不建议直接继承该方法，而建议继承 doFilter(HttpServletRequest request,
     * HttpServletResponse response, FilterChain chain) 方法
     * </p>
     *
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
     *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
     * @param req
     * @param resp
     * @param chain
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        doFilter(request, response, chain);

    }

    /**
     * @Title: doFilter
     * @Description: TODO 抽象方法，为Http请求定制，必须实现的方法。
     * @param request
     * @param response
     * @param chain
     * @throws IOException
     * @throws ServletException
     * @return void
     */
    public abstract void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
            throws IOException, ServletException;

}
